package com.markc1d.weibo.controller;

import com.markc1d.weibo.mapper.BlogMapper;
import com.markc1d.weibo.mapper.HotNewsMapper;
import com.markc1d.weibo.pojo.dto.BlogDto;
import com.markc1d.weibo.pojo.entity.Blog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.rmi.CORBA.Util;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping
public class BlogController {
    @Autowired
    BlogMapper blogMapper;

    @GetMapping("/BlogArr")
    public List<Blog> BlogArr() {
        return blogMapper.BlogArr();
    }

    @GetMapping("admin/BlogArr")
    public List<Blog> AdminBlogArr() {
        return blogMapper.BlogArr();
    }

    @PutMapping("admin/UpdateBlog/{id}")
    public int updateBlog(@PathVariable("id") Integer id, @RequestBody Blog blog) {
        // 设置博客的 ID，以便在更新时使用
        blog.setId(id);
        // 调用 BlogMapper 的 updateBlog 方法来更新博客信息
        int result = blogMapper.updateBlog(blog);
        // 根据更新结果返回适当的响应
        if (result > 0) {
            return 1;
        } else {
            return 2;
        }
    }

    @DeleteMapping("admin/DeleteBlog/{id}")
    public int deleteBlog(@PathVariable("id") Integer id) {
        int result = blogMapper.deleteBlog(id);
        if (result > 0) {
            return 1;
        } else {
            return 2;
        }
    }

    @RequestMapping("/AddBlog")
    public int AddBlog(@RequestBody BlogDto addblog) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(addblog, blog);
        blog.setCreateTime(new Date());
        blog.setUsername("微博用户" + Math.random());
        blog.setFollower(Integer.valueOf("1"));
        blog.setUserImg("https://picsum.photos/300/300");
        blog.setContentImg("https://picsum.photos/500/300");
        blogMapper.insertBlog(blog);
        return 0;
    }
}
